﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Auros.Categorisation.Data
{
    public class CategoryCache
    {
        private const string CACHE_KEY = "Categorisation:Categories";
        private static object cacheLocker = new object();

        public static List<Category> Get()
        {
            if (HttpRuntime.Cache[CACHE_KEY] == null)
            {
                return new List<Category>();
            }

            return (List<Category>)HttpRuntime.Cache[CACHE_KEY];
        }

        public static void Add(IEnumerable<Category> categories)
        {
            lock (cacheLocker)
            {
                if (HttpRuntime.Cache[CACHE_KEY] == null)
                {
                    HttpRuntime.Cache[CACHE_KEY] = categories.ToList();
                }
            }
        }

        public static void Clear()
        {
            HttpRuntime.Cache.Remove(CACHE_KEY);
        }
    }
}